import * as Koa from "koa";
import bodyParser from "koa-bodyparser";
import KoaRouter from 'koa-router';
import {connect} from 'mongoose';
import indexRoutes from '@/routes/index';

import { errorHandler } from "@/middleware/errorHandler";
import ctxExtend from '@/middleware/ctxExtend';

import {mongodb} from '@/config/db';
const app = new Koa.default();
const router = new KoaRouter();
indexRoutes(router);
app
    .use(errorHandler)
    .use(ctxExtend)  
    .use(bodyParser())
    .use(router.routes())
    .use(router.allowedMethods());

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    connect(`mongodb://${mongodb.host}:${mongodb.port || '27017'}/${mongodb.db}?replicaSet=rs0`)
        .then(() => console.log('Connected to MongoDB'))
        .catch(err => console.error('Connection error:', err));
});
